package com.zhou.juc.test03_volatile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouyanxiang
 * @create 2020-08-2020/8/11-13:32
 * volatile 只能解决指令重排序问题，不能解决同步锁问题，因此不能替代synchronized
 */
public class TestVolatile {

    volatile int  count = 0;

    public void m(){
        for (int i = 0; i < 10000; i++) {
            count++;
        }
    }

    public static void main(String[] args) {
        TestVolatile testVolatile = new TestVolatile();

        List<Thread> threads = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            threads.add(new Thread(testVolatile::m,"thread---" + i));
        }

        threads.forEach((o)->o.start());

        threads.forEach((o)->{
            try {
                o.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        System.out.println(testVolatile.count);
    }

}
